'use strict';

const {Controller} = require('egg');

class classController extends Controller {
  async add() {
    const {ctx} = this;
    const req = ctx.request.body;
    const res = await ctx.service.classService.add(
        req.name,
        req.head,
        req.u_id
    );
    ctx.body = ctx.helper.json(
        res.data ? res.data : null,
        res.suc ? 200 : 500,
        res.msg
    );
  }

  async joinClass() {
    const {ctx} = this;
    const req = ctx.request.body;
    const res = await ctx.service.classService.joinClass(
        req.u_id,
        req.c_id,
        req.isTeacher,
        req.in_review
    );
    ctx.body = ctx.helper.json(
        res.data ? res.data : null,
        res.suc ? 200 : 500,
        res.msg
    );
  }

  async del() {
    const {ctx} = this;
    const req = ctx.request.body;
    const res = await ctx.service.classService.del(req.id);
    ctx.body = ctx.helper.json(
        res.data ? res.data : null,
        res.suc ? 200 : 500,
        res.msg
    );
  }

  async exitClass() {
    const {ctx} = this;
    const req = ctx.request.body;
    const res = await ctx.service.classService.exitClass(
        req.u_id,
        req.c_id
    );
    ctx.body = ctx.helper.json(
        res.data ? res.data : null,
        res.suc ? 200 : 500,
        res.msg
    );
  }

  /**
   * @description: 更新班级的code
   */
  async updateCode() {
    const {ctx} = this;
    const req = ctx.request.body;
    const newCode = ctx.helper.getRandomCode();
    const res = await ctx.service.classService.update({
      id: req.id,
      code: newCode,
    });
    ctx.body = ctx.helper.json(
        res.data ? res.data : null,
        res.suc ? 200 : 500,
        res.msg
    );
  }

  /**
   * @description: 更新班级的name
   */
  async updateName() {
    const {ctx} = this;
    const req = ctx.request.body;
    const newCode = ctx.helper.getRandomCode();
    const res = await ctx.service.classService.updateName({
      id: req.id,
      name: req.name,
    });
    ctx.body = ctx.helper.json(
        res.data ? res.data : null,
        res.suc ? 200 : 500,
        res.msg
    );
  }

  async getOne() {
    const {ctx} = this;
    const req = ctx.request.body;
    const res = await ctx.service.classService.getOne(req);
    ctx.body = ctx.helper.json(
        res.data ? res.data : null,
        res.suc ? 200 : 500,
        res.msg
    );
  }

  async userGetAll() {
    const {ctx} = this;
    const req = ctx.request.body;
    const res = await ctx.service.classService.userGetAll(req.u_id);
    ctx.body = ctx.helper.json(
        res.data ? res.data : null,
        res.suc ? 200 : 500,
        res.msg
    );
  }

  async userManageClass() {
    const {ctx} = this;
    const req = ctx.request.body;
    const res = await ctx.service.classService.userManageClass(req.u_id);
    ctx.body = ctx.helper.json(
        res.data ? res.data : null,
        res.suc ? 200 : 500,
        res.msg
    );
  }

  async isUserManage() {
    const {ctx} = this;
    const req = ctx.request.body;
    const res = await ctx.service.classService.isUserManage(req.u_id, req.c_id);
    ctx.body = ctx.helper.json(
        res.data ? res.data : null,
        res.suc ? 200 : 500,
        res.msg
    );
  }

  async getClassAllUser() {
    const {ctx} = this;
    const req = ctx.request.body;
    const res = await ctx.service.classService.getClassAllUser(req.id);
    ctx.body = ctx.helper.json(
        res.data ? res.data : null,
        res.suc ? 200 : 500,
        res.msg
    );
  }

  async getClassAllAdmin() {
    const {ctx} = this;
    const req = ctx.request.body;
    const res = await ctx.service.classService.getClassAllAdmin(req.id);
    ctx.body = ctx.helper.json(
        res.data ? res.data : null,
        res.suc ? 200 : 500,
        res.msg
    );
  }
}

module.exports = classController;
